
import * as THREE from "three"
class myVector extends THREE.Vector3 {
    speedx: number
    speedy: number
    speedz: number
    constructor(x: number, y: number, z: number) {
        super(x, y, z)
        this.speedx = Math.random() * 0.5 - 0.25
        this.speedy = - Math.random() * 0.5
        this.speedz = Math.random() * 0.5 - 0.25
    }
}
export class MyPoints {
    scene: THREE.Scene
    //顶点信息
    vectices: myVector[]
    geometry: THREE.BufferGeometry
    config
    constructor(options: myPoints) {
        let { scene} = options
        this.config = options
        this.scene = scene
        this.vectices = []
        this.geometry = new THREE.BufferGeometry

        this.init()
    }
    init() {
        let { size, opacity, range,count ,textureUrl, setSpeed, setPosition } = this.config
        this.vectices = []
        for (let index = 0; index < count; index++) {
            const x = THREE.MathUtils.randFloatSpread(range * 2)
            const y = THREE.MathUtils.randFloat(0, range)
            const z = THREE.MathUtils.randFloatSpread(range * 2)
            const vector = new myVector(x, y, z)
            setSpeed(vector)
            setPosition(vector)

            this.vectices.push(vector)
        }


        // this.geometry.setAttribute("position", new THREE.Float32BufferAttribute(this.vectices, 3))
        this.geometry.setFromPoints(this.vectices)
        //    geomerty.setAttribute("name",new THREE.BufferAttribute(vectices,1))
        const material = new THREE.PointsMaterial({
            size: size,
            map: new THREE.TextureLoader().load(textureUrl),
            transparent: true,
            opacity: opacity,
            depthTest: false
        })
        const points = new THREE.Points(this.geometry, material)
        points.name = 'snow'
        this.scene.add(points)
    }
    animation() {

        this.vectices.forEach(v => {
            this.config.setAnimation(v)
        })

        this.geometry.setFromPoints(this.vectices)
    }
}